package org.agilelang.generator.cakephp;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.agilelang.object.attributes.Attribute;
import org.agilelang.object.attributes.Event;
import org.agilelang.object.attributes.Reference;
import org.agilelang.object.Controller;
import org.agilelang.object.Model;
import org.agilelang.object.Project;

public class GControllerTransaction extends GControllerSimple {

    String menu = "";
    String menu2 = "";
    String submenu = "";
    Model model_header, model_detail;

    public GControllerTransaction(String path, Project project, Controller controller) {//String appname,  List<Controller> listController, List<Menu> menus) {
        super(path, project, controller);
        this.project = project;
        this.template = "bootstrap";
    }

    @Override
    public void process() {
        String model_header_name = controller.model_header;
        String model_detail_name = controller.model_detail;
        List<Model> models = project.models;
        int t = models.size();
        for (int x = 0; x < t; x++) {
            Model model = models.get(x);
            if (model.name.equals(model_header_name)) {
                model_header = model;
            }
            if (model.name.equals(model_detail_name)) {
                model_detail = model;
            }
        }
        this.createDirectory(path + File.separator + "view/" + controller.name);
        //menu2 = search_menu2(controller.name, "crud");
        menu = searchMenu(controller.name, "crud");
        submenu = searchMenuItem(controller.name, "crud");
        controller_php();
        controller_index();
        controller_create();
        controller_read();
    }

    private void controller_php() {
        String ref_header = "",
                import_header = "",
                param_header = "",
                js_save_header = "",
                py_save_ref_header = "",
                py_save_header = "";
        List<Attribute> attributes_header = model_header.attributes;
        int t = attributes_header.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_header.get(x);
            js_save_header = js_save_header + "" + attr.name + " : $(\"#" + attr.name + "\").val(), ";
            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_header.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {
                        ref_header += "\t\t$this->set('" + this.setPluralize(ref.model_reference).toLowerCase() + "', $this->" + ref.model_reference + "->find('list'));\n";

                        import_header = import_header + "'" + ref.model_reference + "',";
                        py_save_ref_header += "\t\t\t\t'" + attr.name + "'	=> $_POST[\"" + attr.name + "\"],";
                        py_save_header = py_save_header + "\t\t\t" + attr.name + " = " + ref.model_reference.toLowerCase() + ".key(),\n";
                    }
                }
            } else {
                if (attr.type.equals("date")) {
                    py_save_ref_header += "\t\t\t\t'" + attr.name + "'	=> $_POST[\"" + attr.name + "\"],";
                } else if (attr.type.equals("int") || attr.type.equals("integer")) {
                    py_save_ref_header += "\t\t\t\t'" + attr.name + "'	=> $_POST[\"" + attr.name + "\"],";
                } else if (attr.type.equals("float")) {
                    py_save_ref_header += "\t\t\t\t'" + attr.name + "'	=> $_POST[\"" + attr.name + "\"],";
                } else {
                    py_save_ref_header += "\t\t\t\t'" + attr.name + "'	=> $_POST[\"" + attr.name + "\"],";
                }
            }
        }

        String table_th_detail = "", js_detail = "", js_td_detail = "", js_save_detail = "", py_save_ref_detail = "", py_save_detail = "";
        List<Attribute> attributes_detail = model_detail.attributes;
        t = attributes_detail.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_detail.get(x);
            if (attr.name.equals(controller.relation_detail)) {
                continue;
            }
            table_th_detail = table_th_detail + "\t\t\t\t\t\t<th>" + attr.caption + "</th>\n";
            js_detail = js_detail + "var " + attr.name + " = $(\"#" + attr.name + "\");\n";
            js_td_detail = js_td_detail + "\"<td class=\\\"" + attr.name + "\\\">\" + " + attr.name + ".val() + \"</td>\" +\n";
            js_save_detail = js_save_detail + attr.name + ": $(this).children(\"." + attr.name + "\").html(), ";
            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_detail.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {

                        ref_header += "\t\t$this->set('" + this.setPluralize(ref.model_reference).toLowerCase() + "', $this->" + ref.model_reference
                                + "->find('list', array('fields' => array('" + ref.model_reference + ".id', '" + ref.field_reference + "' ))));\n";

                        import_header = import_header + "'" + ref.model_reference + "',";

                        py_save_detail += "\t\t\t\t\t\t'" + attr.name + "'	=> $_POST[\"detail\"][$pos]['" + attr.name + "'],\n";
                    }
                }

            } else {
                if (attr.type.equals("date")) {
                    py_save_detail += "\t\t\t\t\t\t'" + attr.name + "'	=> $_POST[\"detail\"][$pos]['" + attr.name + "'],\n";
                } else if (attr.type.equals("int") || attr.type.equals("integer")) {
                    py_save_detail += "\t\t\t\t\t\t'" + attr.name + "'	=> $_POST[\"detail\"][$pos]['" + attr.name + "'],\n";
                } else if (attr.type.equals("float")) {
                    py_save_detail += "\t\t\t\t\t\t'" + attr.name + "'	=> $_POST[\"detail\"][$pos]['" + attr.name + "'],\n";
                } else {
                    py_save_detail += "\t\t\t\t\t\t'" + attr.name + "'	=> $_POST[\"detail\"][$pos]['" + attr.name + "'],\n";
                }
            }
        }


        import_header = "'" + controller.model_header + "'," + import_header + "'" + controller.model_detail + "'";

        //String header_index = "\t\t$this->set('" + this.setPluralize(controller.model_header).toLowerCase() + "', $this->" + controller.model_header + "->find('all'));";
        String header_index = "\t\t$this->set('" + this.setPluralize(controller.model_header).toLowerCase() + "', $this->paginate());";
        //String header_update = "\t\t$this->set('" + this.setPluralize(controller.model_header).toLowerCase() + "', $this->" + controller.model_header + "->find('all'));";

        String views = "";
        views += view(controller.views, controller.name);
        views += viewform(controller.forms, controller.name);
        views += viewreport(controller.reports, controller.name);

        String file = readStreamTpl("ct_controller.php.tpl");
        file = file.replace("//views_str//", views);
        file = file.replace("//Header//", controller.model_header);
        file = file.replace("//header//", controller.model_header.toLowerCase());
        file = file.replace("//Detail//", controller.model_detail);
        file = file.replace("//detail//", controller.model_detail.toLowerCase());
        file = file.replace("//details//", this.setPluralize(controller.model_detail).toLowerCase());
        file = file.replace("//relation//", controller.relation_detail);
        file = file.replace("//controller//", controller.name.toLowerCase());


        file = file.replace("//menu//", menu);
        file = file.replace("//submenu//", submenu);
        file = file.replace("//header_index//", header_index);

        file = file.replace("//import_header//", import_header);
        file = file.replace("//ref_header//", ref_header);
        file = file.replace("//param_header//", param_header);
        file = file.replace("//NameClass//", controller.name);
        file = file.replace("//py_save_header//", py_save_ref_header);
        file = file.replace("//py_save_detail//", py_save_detail);
        file = file.replace("//menu//", menu);
        createfile("controller/" + controller.name + "Controller.php", path, file);

    }

    private void controller_index() {
        String th = "", td = "";
        List<Attribute> attributes_header = model_header.attributes;
        Collections.sort(attributes_header, new CustomComparator2());
        int t = attributes_header.size();

        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_header.get(x);
            //th = th + "<th><?php echo $this->Paginator->sort('" + attr.caption + "');?></th>";
            th = th + "<th>" + attr.caption + "</th>";
            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_header.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {
                        td = td + "<td><?php echo $" + model_header.name.toLowerCase() + "['" + ref.model_reference + "']['" + ref.field_reference + "']; ?></td>";

                    }
                }
            } else {
                td = td + "<td><?php echo $" + model_header.name.toLowerCase() + "['" + model_header.name + "']['" + attr.name + "']; ?></td>";

            }
        }

        String menu_1 = searchMenu(controller.name, "crud");
        String menu_2 = searchMenuItem(controller.name, "crud");

        String file = readStreamTpl("ct_index.html.tpl");
        file = file.replace("//th//", th);
        file = file.replace("//td//", td);
        file = file.replace("//menu//", menu);
        file = file.replace("//menu_1//", menu_1);
        file = file.replace("//menu_2//", menu_2);
        file = file.replace("//submenu//", submenu);
        file = file.replace("//Header//", model_header.name);
        file = file.replace("//HeaderCaption//", model_header.caption);
        file = file.replace("//header//", model_header.name.toLowerCase());
        file = file.replace("//headers//", this.setPluralize(model_header.name).toLowerCase());
        file = file.replace("//Controller//", controller.name);
        createfile("view/" + controller.name.toLowerCase() + "/index.ctp", path, file);
    }

    private void controller_create() {
        String attributes_model_header = "", js_save_header = "";
        List<Attribute> attributes_header = model_header.attributes;
        Collections.sort(attributes_header, new CustomComparator2());
        int t = attributes_header.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_header.get(x);
            js_save_header = js_save_header + "" + attr.name + " : $(\"#" + attr.name + "\").val(), ";






            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_header.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {
                        attributes_model_header = attributes_model_header
                                + "<?php echo $this->Form->input('" + attr.name + "', array( 'empty'=>' - selecione - ','id'=>'" + attr.name + "','div'=>'control-group','after' => '</div>','between' => '<div class=\"controls\">', 'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'placeholder' => '" + attr.caption + "'));?>"
                                + "\n";
                    }
                }
            } else {
                if (attr.type.equals("date")) {
                    attributes_model_header = attributes_model_header
                            + "<?php echo $this->Form->input('" + attr.name + "', array( 'id'=>'" + attr.name + "','div'=>'control-group','after' => '</div>','between' => '<div class=\"controls\">','class' => 'datepicker','label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'placeholder' => '" + attr.caption + "' ));?>"
                            + "\n";
                } else if (attr.type.equals("int") || attr.type.equals("integer")) {
                    attributes_model_header = attributes_model_header
                            + "<?php echo $this->Form->input('" + attr.name + "', array( 'id'=>'" + attr.name + "','div'=>'control-group','after' => '</div>','between' => '<div class=\"controls\">', 'value'=>'0','label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'placeholder' => '" + attr.caption + "'));?>"
                            + "\n";
                } else if (attr.type.equals("float") || attr.type.equals("double")) {
                    attributes_model_header = attributes_model_header
                            + "<?php echo $this->Form->input('" + attr.name + "', array( 'id'=>'" + attr.name + "','div'=>'control-group','after' => '</div>','between' => '<div class=\"controls\">', 'value'=>'0','label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'placeholder' => '" + attr.caption + "' ));?>"
                            + "\n";
                } else {
                    attributes_model_header = attributes_model_header
                            + "<?php echo $this->Form->input('" + attr.name + "', array( 'id'=>'" + attr.name + "','div'=>'control-group','after' => '</div>','between' => '<div class=\"controls\">','label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'placeholder' => '" + attr.caption + "' ));?>"
                            + "\n";
                }
            }
        }

        String table_th_detail = "", attributes_model_detail = "", js_detail = "", js_detail_ini = "", js_td_detail = "", js_save_detail = "", py_save_ref_detail = "", py_save_detail = "";
        List<Attribute> attributes_detail = model_detail.attributes;
        Collections.sort(attributes_detail, new CustomComparator2());
        t = attributes_detail.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_detail.get(x);
            if (attr.name.equals(controller.relation_detail)) {
                continue;
            }

            js_detail = js_detail + "var " + attr.name + " = $(\"#" + attr.name + "\");\n";
            js_detail_ini = js_detail_ini + "\t\t$(\"#d_" + attr.name + "\").val('');\n";

            js_save_detail = js_save_detail + attr.name + ": $(this).children(\"." + attr.name + "\").html(), ";
            if (attr.name.endsWith("_id")) {
                js_td_detail = js_td_detail + "\t\t\t\t\t\"<td class=\\\"" + attr.name + "\\\">\" + $(\"#d_" + attr.name + "\").val() +\"</td>\" +\n";
                js_td_detail = js_td_detail + "\t\t\t\t\t\"<td>\" + $(\"#d_" + attr.name + " option:selected\").text() + \"</td>\" +\n";
                table_th_detail = table_th_detail + "\t\t\t\t\t\t<th>" + attr.caption + " ID</th>\n";
                table_th_detail = table_th_detail + "\t\t\t\t\t\t<th>" + attr.caption + "</th>\n";
                List<Reference> reference_header = model_detail.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {


                        py_save_ref_detail = py_save_ref_detail + "\t\t\t" + ref.model_reference.toLowerCase() + " = db.get(db.Key.from_path('" + ref.model_reference + "', int(self.request.POST['detail['+str(pos)+'][" + attr.name + "]'])))\n";
                        py_save_detail = py_save_detail + "\t\t\t\t" + attr.name + " = " + ref.model_reference.toLowerCase() + ".key(),\n";

                        attributes_model_detail = attributes_model_detail
                                + "<?php echo "
                                + "$this->Form->input('d_" + attr.name + "', "
                                + "array( 'id'=>'d_" + attr.name + "',"
                                + "'options' => $" + this.setPluralize(ref.model_reference).toLowerCase() + ","
                                + "'empty' => ' - selecione - ',"
                                + "'div' => 'control-group',"
                                + "'after' => '</div>',"
                                + "'between' => '<div class=\"controls\">',"
                                + "'placeholder' => '" + attr.caption + "',"
                                + "'label' => array('text' => '" + attr.caption + "', 'class' => 'control-label' )));?>"
                                + "\n";
                    }
                }

            } else {
                js_td_detail = js_td_detail + "\t\t\t\t\t\"<td class=\\\"" + attr.name + "\\\">\" + $(\"#d_" + attr.name + "\").val() + \"</td>\" +\n";
                table_th_detail = table_th_detail + "\t\t\t\t\t\t<th>" + attr.caption + "</th>\n";
                if (attr.type.equals("date")) {

                    attributes_model_detail = attributes_model_detail
                            + "<?php echo "
                            + "$this->Form->input("
                            + "'d_" + attr.name + "', "
                            + "array( 'id'=>'d_" + attr.name + "',"
                            + "'div'=>'control-group',"
                            + "'after' => '</div>',"
                            + "'between' => '<div class=\"controls\">',"
                            + "'placeholder' => '" + attr.caption + "',"
                            + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label' )));?>"
                            + "\n";
                } else if (attr.type.equals("int") || attr.type.equals("integer")) {

                    attributes_model_detail = attributes_model_detail
                            + "<?php echo "
                            + "$this->Form->input("
                            + "'d_" + attr.name + "', "
                            + "array( 'id'=>'d_" + attr.name + "',"
                            + "'div'=>'control-group',"
                            + "'after' => '</div>',"
                            + "'between' => '<div class=\"controls\">',"
                            + "'placeholder' => '" + attr.caption + "',"
                            + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label' )));?>"
                            + "\n";
                } else if (attr.type.equals("float")) {

                    attributes_model_detail = attributes_model_detail
                            + "<?php echo "
                            + "$this->Form->input('d_" + attr.name + "', "
                            + "array( 'id'=>'d_" + attr.name + "',"
                            + "'div'=>'control-group',"
                            + "'after' => '</div>',"
                            + "'between' => '<div class=\"controls\">',"
                            + "'placeholder' => '" + attr.caption + "',"
                            + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label' )));?>"
                            + "\n";
                } else if (attr.type.equals("double")) {

                    attributes_model_detail = attributes_model_detail
                            + "<?php echo "
                            + "$this->Form->input('d_" + attr.name + "', "
                            + "array( 'id'=>'d_" + attr.name + "',"
                            + "'div'=>'control-group',"
                            + "'after' => '</div>',"
                            + "'between' => '<div class=\"controls\">',"
                            + "'placeholder' => '" + attr.caption + "',"
                            + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label' )));?>"
                            + "\n";
                } else {

                    attributes_model_detail = attributes_model_detail
                            + "<?php echo $this->Form->input('d_" + attr.name + "', "
                            + "array( 'id'=>'d_" + attr.name + "',"
                            + "'div'=>'control-group',"
                            + "'after' => '</div>',"
                            + "'between' => '<div class=\"controls\">',"
                            + "'placeholder' => '" + attr.caption + "',"
                            + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label' )));?>"
                            + "\n";
                }
            }
        }

        this.createDirectory(path + File.separator + "view/" + controller.name.toLowerCase());
        String view_file = readStreamTpl("ct_create.html.tpl");
        view_file = view_file.replace("//NameClass//", controller.name);
        view_file = view_file.replace("//nameCaption//", controller.caption);
        view_file = view_file.replace("//HeaderCaption//", model_header.caption);
        view_file = view_file.replace("//nameclass//", controller.name.toLowerCase());
        view_file = view_file.replace("//attributes_model_header//", attributes_model_header);
        view_file = view_file.replace("//attributes_model_detail//", attributes_model_detail);
        view_file = view_file.replace("//table_th_detail//", table_th_detail);
        createfile("view/" + controller.name.toLowerCase() + "/create.ctp", path, view_file);
        String selectDetail = "";
        String addDetail = "";
        String deleteDetail = "";
        String updateDetail = "";

        String event_insertDetail = "";
        List<Event> events = controller.events;
        int te = events.size();
        for (int x = 0; x < te; x++) {
            Event event = events.get(x);
            String action = event.action;


            action = action.replaceAll(" ", "") + " );";
            Pattern pattern = Pattern.compile("(header\\.)(.+?)(\\:)");
            Matcher matcher = pattern.matcher(action);
            List<String> listMatches = new ArrayList<String>();
            while (matcher.find()) {
                System.out.println(matcher.group(2));
                listMatches.add(matcher.group(2));
            }
            HashSet hs = new HashSet();
            hs.addAll(listMatches);
            listMatches.clear();
            listMatches.addAll(hs);
            for (int i = 0; i < listMatches.size(); i++) {
                System.out.println(listMatches.get(i).toString());
                String new_str = "\\$('#" + listMatches.get(i).toString() + "').val( ";
                action = action.replaceAll("header." + listMatches.get(i).toString() + ":", new_str);
            }

            System.out.println(action);
            //----------------------------------------------------------------------
            pattern = Pattern.compile("(header\\.)(.+?)(\\s|\\+|\\-|\\*|\\/)");
            matcher = pattern.matcher(action);
            listMatches = new ArrayList<String>();
            while (matcher.find()) {
                System.out.println(matcher.group(2));
                listMatches.add(matcher.group(2));
            }
            hs = new HashSet();
            hs.addAll(listMatches);
            listMatches.clear();
            listMatches.addAll(hs);
            for (int i = 0; i < listMatches.size(); i++) {
                System.out.println(listMatches.get(i).toString());
                String new_str = " parseFloat(\\$('#" + listMatches.get(i).toString() + "').val()) ";
                action = action.replaceAll("header." + listMatches.get(i).toString(), new_str);
            }

            System.out.println(action);
            //----------------------------------------------------------------------
            pattern = Pattern.compile("(detail\\.)(.+?)(\\s|\\+|\\-|\\*|\\/)");
            matcher = pattern.matcher(action);
            listMatches = new ArrayList<String>();
            while (matcher.find()) {
                System.out.println(matcher.group(2));
                listMatches.add(matcher.group(2));
            }
            hs = new HashSet();
            hs.addAll(listMatches);
            listMatches.clear();
            listMatches.addAll(hs);


            if (event.type.equals("selectDetail")) {
                selectDetail += action + "\n";
            }
            if (event.type.equals("addDetail")) {
                for (int i = 0; i < listMatches.size(); i++) {
                    System.out.println(listMatches.get(i).toString());
                    String new_str = " parseFloat(\\$('#d_" + listMatches.get(i).toString() + "').val()) ";
                    action = action.replaceAll("detail." + listMatches.get(i).toString(), new_str);
                }
                action = action.replaceAll(":", "=");
                System.out.println(action);
                addDetail += action + "\n";
            }
            if (event.type.equals("deleteDetail")) {
                for (int i = 0; i < listMatches.size(); i++) {
                    System.out.println(listMatches.get(i).toString());
                    String new_str = " parseFloat(tr.children(\"." + listMatches.get(i).toString() + "\").html()) ";
                    action = action.replaceAll("detail." + listMatches.get(i).toString(), new_str);
                }
                action = action.replaceAll(":", "=");
                System.out.println(action);
                deleteDetail += action + "\n";
            }
            if (event.type.equals("plus_update")) {
                updateDetail += action + "\n";
            }
        }

        String menu_1 = searchMenu(controller.name, "crud");
        String menu_2 = searchMenuItem(controller.name, "crud");

        String js_file = readStreamTpl("ct_index.js.tpl");
        js_file = js_file.replace("//js_detail//", "");//js_detail
        js_file = js_file.replace("//js_detail_ini//", js_detail_ini);
        js_file = js_file.replace("//js_td_detail//", js_td_detail);
        js_file = js_file.replace("//js_save_detail//", js_save_detail);
        js_file = js_file.replace("//js_save_header//", js_save_header);
        js_file = js_file.replace("//event_insertDetail//", event_insertDetail);
        js_file = js_file.replace("//addDetail//", addDetail);
        js_file = js_file.replace("//deleteDetail//", deleteDetail);
        js_file = js_file.replace("//updateDetail//", updateDetail);
        js_file = js_file.replace("//menu_1//", menu_1);
        js_file = js_file.replace("//menu_2//", menu_2);

        createfile("/webroot/js/_" + controller.name.toLowerCase() + ".js", path, js_file);

    }

    private void controller_read() {
        String th = "", td = "", header = "";
        List<Attribute> attributes_header = model_header.attributes;
        int t = attributes_header.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_header.get(x);
            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_header.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {
                        /*header = header
                         + "<?php echo $this->Form->input('" + attr.name + "', array( 'empty'=>' - selecione - ','id'=>'" + attr.name + "','label' => '" + attr.caption + "','class' => 'text ui-widget-content ui-corner-all','style'=>'width: 215px','DISABLED'=>'DISABLED','value'=>$" + model_header.name.toLowerCase() + "['" + ref.model_reference + "']['" + ref.field_reference + "'] ));?>"
                         + "\n";*/

                        header = header + "<?php echo $this->Form->input('" + attr.name + "', array('label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'placeholder' => '" + attr.caption + "','disabled' => 'disabled','value'=>$" + model_header.name.toLowerCase() + "['" + ref.model_reference + "']['" + ref.field_reference + "'])); ?>\r\n";

                    }
                }
            } else {
                /*header = header
                 + "<?php echo $this->Form->input('" + attr.name + "', array( 'id'=>'" + attr.name + "','label' => '" + attr.caption + "','class' => 'datepicker text ui-widget-content ui-corner-all','style'=>'width: 200px','DISABLED'=>'DISABLED' ,'value'=>$" + model_header.name.toLowerCase() + "['" + model_header.name + "']['" + attr.name + "']));?>"
                 + "\n";*/
                header = header + "<?php echo $this->Form->input('" + attr.name + "', array('label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'placeholder' => '" + attr.caption + "','disabled' => 'disabled','value'=>$" + model_header.name.toLowerCase() + "['" + model_header.name + "']['" + attr.name + "'])); ?>\r\n";



            }
        }
        List<Attribute> attributes_detail = model_detail.attributes;
        t = attributes_detail.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_detail.get(x);
            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_detail.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {
                        //td = td + "<td>{{" + model_detail.name.toLowerCase() + "." + attr.name + "." + ref.field_reference + "}}</td>";
                        th = th + "<th>" + attr.caption + "</th>";
                        td = td + "<td><?php echo $" + model_detail.name.toLowerCase() + "['" + ref.model_reference + "']['" + ref.field_reference + "']; ?></td>";

                    }
                }
            } else {
                //td = td + "<td>{{" + model_detail.name.toLowerCase() + "." + attr.name + "}}</td>";
                td = td + "<td><?php echo $" + model_detail.name.toLowerCase() + "['" + model_detail.name + "']['" + attr.name + "']; ?></td>";

                th = th + "<th>" + attr.caption + "</th>";
            }
        }
        String file = readStreamTpl("ct_read.html.tpl");
        file = file.replace("//th//", th);
        file = file.replace("//td//", td);
        file = file.replace("//menu//", menu);
        file = file.replace("//submenu//", submenu);
        file = file.replace("//header//", header);
        file = file.replace("//header_name//", model_header.name);
        file = file.replace("//detail//", model_detail.name.toLowerCase());
        file = file.replace("//details//", this.setPluralize(model_detail.name).toLowerCase());
        createfile("view/" + controller.name.toLowerCase() + "/view.ctp", path, file);
    }

    public class CustomComparator2 implements Comparator<Attribute> {

        @Override
        public int compare(Attribute o1, Attribute o2) {
            return o1.item.compareTo(o2.item);
        }
    }
}
